package com.yc.sole.framework.result;

import lombok.Data;

import java.io.Serializable;

/**
 * 统一返回结果封装
 *
 * @author yizuomin
 * @date 2022/11/19 13:14
 **/
@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 8793770316213528277L;

    /**
     * 返回code
     */
    private Integer code;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 是否成功
     */
    private boolean success;
    /**
     * 返回数据
     */
    private T data;

    public Result() {
    }

    private Result(IResultMsg iResultCode) {
        this.code = iResultCode.getCode();
        this.message = iResultCode.getMessage();
    }

    private Result(IResultMsg resultCode, T data) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
        this.data = data;
        this.success = BaseResultEnum.SUCCESS.getCode().equals(code);
    }

    private Result(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    private Result(Integer code, String message, boolean success, T data) {
        this.code = code;
        this.message = message;
        this.success = success;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return new Result<>(BaseResultEnum.SUCCESS.getCode(), BaseResultEnum.SUCCESS.getMessage(), Boolean.TRUE);
    }

    public static <T> Result<T> success(String message) {
        return new Result<>(BaseResultEnum.SUCCESS.getCode(), message, Boolean.TRUE);
    }

    public static <T> Result<T> data(T data) {
        Integer code = BaseResultEnum.SUCCESS.getCode();
        String message = BaseResultEnum.SUCCESS.getMessage();
        return new Result<>(code, message, true, data);
    }

    public static <T> Result<T> data(IResultMsg resultCode, T data) {
        return new Result<>(resultCode, data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(BaseResultEnum.FAIL.getCode(), BaseResultEnum.FAIL.getMessage(), Boolean.FALSE);
    }

    public static <T> Result<T> fail(IResultMsg resultCode) {
        return new Result<>(resultCode.getCode(), resultCode.getMessage(), Boolean.FALSE);
    }

    public static <T> Result<T> fail(Integer code, String message) {
        return new Result<>(code, message, Boolean.FALSE);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(BaseResultEnum.FAIL.getCode(), message, Boolean.FALSE);
    }

    public static <T> Result<T> error(String message) {
        return new Result<>(BaseResultEnum.ERROR.getCode(), message, Boolean.FALSE);
    }

    public static <T> Result<T> error() {
        return new Result<>(BaseResultEnum.ERROR.getCode(), BaseResultEnum.ERROR.getMessage(), Boolean.FALSE);
    }

    public static <T> Result<T> status(Boolean flag) {
        return flag ? success() : fail();
    }
}
